package hr.fer.ztel.rassus.brbljaonica.gui;

import hr.fer.ztel.rassus.brbljaonica.business.BusinessProvider;
import hr.fer.ztel.rassus.brbljaonica.business.HostedRoomPresentation;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import org.jivesoftware.smack.Chat;

import javax.swing.ListSelectionModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

/**
 * Klasa koja predstavlja glavni panel aplikacije<br>
 * Sastoji se od liste online kontakata te liste dostupnih soba<br>
 * Kontakti se mogu dodavati i brisati, a mogu se i kreirati nove sobe
 * 
 */
public class MainAppPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList buddyList;
	private JList roomList;
	private boolean isFirstTime;

	public MainAppPanel() {
		isFirstTime = true;
		setLayout(null);
		
		JScrollPane buddyListScrollPane = new JScrollPane();
		buddyListScrollPane.setBounds(10, 39, 215, 250);
		add(buddyListScrollPane);
		
		buddyList = new JList();
		buddyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		buddyList.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					if (buddyList.getSelectedIndex() == -1) return;
					new IndividualChatWindow((String) buddyList.getSelectedValue());
				}
			}
		});
		buddyListScrollPane.setViewportView(buddyList);
		
		JScrollPane roomListScrollPane = new JScrollPane();
		roomListScrollPane.setBounds(235, 39, 205, 250);
		add(roomListScrollPane);
		
		roomList = new JList();
		roomList.setLocation(0, 48);
		roomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		roomList.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
			}
			@Override
			public void mousePressed(MouseEvent e) {
			}
			@Override
			public void mouseExited(MouseEvent e) {
			}
			@Override
			public void mouseEntered(MouseEvent e) {
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					if (roomList.getSelectedIndex() == -1) return;
					new RoomChatWindow((HostedRoomPresentation) roomList.getSelectedValue());
				}
			}
		});
		roomListScrollPane.setViewportView(roomList);
		
		JButton btnCreateRoom = new JButton("Create Room");
		btnCreateRoom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog("Insert name");
				BusinessProvider.createMUC(name);
			}
		});
		btnCreateRoom.setBounds(235, 300, 100, 23);
		add(btnCreateRoom);
		
		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DefaultListModel roomsModel = new DefaultListModel();
				for (HostedRoomPresentation room: BusinessProvider.getAvailableRooms()) {
					roomsModel.addElement(room);
				}
				roomList.setModel(roomsModel);
				roomList.validate();
				if (isFirstTime) {
					BusinessProvider.setRosterListener(MainAppPanel.this);
					BusinessProvider.setChatListener(MainAppPanel.this);
					BusinessProvider.setFileTransferListener(MainAppPanel.this);
					isFirstTime = false;
				}
			}
		});
		btnRefresh.setBounds(351, 300, 89, 23);
		add(btnRefresh);
		
		JButton btnAddContact = new JButton("Add Contact");
		btnAddContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String jid = JOptionPane.showInputDialog("Contact JID");
				
				if (jid != null) {
					BusinessProvider.sendContactRequest(jid);
				}
			}
		});
		btnAddContact.setBounds(10, 300, 93, 23);
		add(btnAddContact);
		
		JButton btnDeleteContact = new JButton("Delete Contact");
		btnDeleteContact.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (buddyList.getSelectedIndex() == -1) {
					JOptionPane.showMessageDialog(MainAppPanel.this, "Select a contact to delete");
				}
				else {
					int result = JOptionPane.showConfirmDialog(
							MainAppPanel.this,
							"Are sure you want to delete the selected contact from your friends list",
							"Delete confirmation",
							JOptionPane.YES_NO_OPTION);

					if (result == JOptionPane.OK_OPTION) {
						BusinessProvider.deleteContact((String) buddyList.getSelectedValue());
					}
					
				}				
			}
		});
		btnDeleteContact.setBounds(120, 300, 105, 23);
		add(btnDeleteContact);
		
		JLabel lblNewLabel = new JLabel("Online friends");
		lblNewLabel.setBounds(10, 14, 73, 14);
		add(lblNewLabel);
		
		JLabel lblAvailableChatRooms = new JLabel("Available chat rooms");
		lblAvailableChatRooms.setBounds(235, 14, 123, 14);
		add(lblAvailableChatRooms);
		
		this.addComponentListener(new ComponentListener() {
			
			@Override
			public void componentShown(ComponentEvent e) {
				DefaultListModel buddyModel = new DefaultListModel();
				for (String entry: BusinessProvider.getOnlineUsers()) {
					buddyModel.addElement(entry);
				}
				buddyList.setModel(buddyModel);
				buddyList.validate();
				
				DefaultListModel roomsModel = new DefaultListModel();
				for (HostedRoomPresentation room: BusinessProvider.getAvailableRooms()) {
					roomsModel.addElement(room);
				}
				roomList.setModel(roomsModel);
				roomList.validate();
				if (isFirstTime) {
					BusinessProvider.setRosterListener(MainAppPanel.this);
					BusinessProvider.setChatListener(MainAppPanel.this);
					BusinessProvider.setFileTransferListener(MainAppPanel.this);
					isFirstTime = false;
				}
			}
			@Override
			public void componentResized(ComponentEvent e) {
			}
			@Override
			public void componentMoved(ComponentEvent e) {
			}
			@Override
			public void componentHidden(ComponentEvent e) {
			}
		});
	}
	
	public JList getBuddyList() {
		return this.buddyList;
	}
	
	public void createNewChatWindow(Chat buddy) {
		new IndividualChatWindow(buddy);
	}
	
	public void createNewChatWindow(String buddy) {
		new IndividualChatWindow(buddy);
	}

	public boolean acceptRequest(String from) {
		int decision = JOptionPane.showConfirmDialog(this, "Accept contact request from " + from, "Contact request", JOptionPane.YES_NO_OPTION);
		if (decision == JOptionPane.YES_OPTION) return true;
		else return false;
	}
}
